package com.web.core.base.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.blue.jdbc.BaseDao;
import com.blue.jdbc.annotation.LockModeType;
import com.web.core.base.model.Sequence;

@Repository
public class SequenceDao extends BaseDao<Sequence>
{
	public SequenceDao()
	{
	}

	@Override
	protected void query(StringBuilder sql, Sequence param)
	{
		if (param != null)
		{
			if (param.getType() != null)
				sql.append(" and a.type=:type");
			
			if (param.getState() != null)
				sql.append(" and a.state=:state");
			
			if (param.getCode() != null && !param.getCode().equals(""))
			{
				sql.append(" and a.code like :code");
				param.setCode("%" + param.getCode() + "%");
			}
			
			if (param.getName() != null && !param.getName().equals(""))
			{
				sql.append(" and a.name like :name");
				param.setName("%" + param.getName() + "%");
			}
		}
	}
	
	public Sequence getByCode(String code)
	{
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("code", code);
		String sql = "select * from base_sequence where code=:code and state=0";
		Sequence sequence = jdbcObjectTemplate.get(Sequence.class, sql, param, LockModeType.WRITE);
		return sequence;
	}
	
	public void updateVal(Integer id, Integer val, Date date)
	{
		if (date == null)
		{
			String sql = "update base_sequence set val=? where id=?";
			jdbcTemplate.update(sql, val, id);
		}
		else
		{
			String sql = "update base_sequence set val=?, date=? where id=?";
			jdbcTemplate.update(sql, val, date, id);
		}
	}
	
}
